using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Counter : MonoBehaviour
{
	public float number = 1000f;

	public float countInSecond = 2f;

	public string format = "0n";

	public bool countOnStart;

	public AudioClip audioCounting;

	public EventTrigger.TriggerEvent customCallback;

	[HideInInspector]
	public AudioSource audioSource;

	private Text textView;

	private float _number;

	private bool onCount;

	private void Awake()
	{
		textView = GetComponent<Text>();
		audioSource = GetComponent<AudioSource>();
		audioSource.Pause();
	}

	private void Start()
	{
		if (countOnStart)
		{
			StartCounter();
		}
	}

	private void FixedUpdate()
	{
		UpdateCounter();
	}

	public void PlayAudio(AudioClip audio)
	{
		audioSource.PlayOneShot(audio);
	}

	public void StartCounter()
	{
		_number = 0f;
		onCount = true;
		textView.text = _number.ToString(format);
		audioSource.Play();
	}

	public void EndCounter()
	{
		textView.text = number.ToString(format);
		onCount = false;
		audioSource.Stop();
		BaseEventData baseEventData = new BaseEventData(EventSystem.current);
		baseEventData.selectedObject = base.gameObject;
		customCallback.Invoke(baseEventData);
	}

	public void UpdateCounter()
	{
		if (onCount)
		{
			_number += number / countInSecond * Time.deltaTime;
			textView.text = _number.ToString(format);
			if (_number >= number)
			{
				EndCounter();
			}
		}
	}
}
